package com.cheetah.start.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.common.quartz.QuartzService;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.module.system.domain.ScheduleJob;
import com.cheetah.start.module.system.mapper.ScheduleJobMapper;
import com.cheetah.start.module.system.service.IScheduleJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Describe: 定时任务服务
 * Author: 就免仪式
 * CreateTime: 2019/10/23
 */
@Slf4j
@Service
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobMapper, ScheduleJob> implements IScheduleJobService {

    /**
     * 引 入 服 务
     */
    @Resource
    private Scheduler scheduler;
    @Autowired
    private ScheduleJobMapper scheduleJobMapper;


    /**
     * Describe: 定时任务列表 分页
     * Param: ScheduleJob PageDomain
     * Return: pageInfo
     */
    @Override
    public Page<ScheduleJob> pageList(ScheduleJob param, PageQuery pageDomain) {
        Page<ScheduleJob> pageInfo = new Page<>(pageDomain.getPage(), pageDomain.getLimit());
        LambdaQueryWrapper<ScheduleJob> queryWrapper = getQueryWrapper(param);
        page(pageInfo, queryWrapper);
        return pageInfo;
    }

    private LambdaQueryWrapper<ScheduleJob> getQueryWrapper(ScheduleJob param) {
        LambdaQueryWrapper<ScheduleJob> queryWrapper = Wrappers.lambdaQuery(ScheduleJob.class);
        queryWrapper.orderByDesc(ScheduleJob::getCreateTime);
        if (param != null) {
            String beanName = param.getBeanName();
            if (StrUtil.isNotBlank(beanName)) {
                queryWrapper.like(ScheduleJob::getBeanName, beanName);
            }
            String jobName = param.getJobName();
            if (StrUtil.isNotBlank(jobName)) {
                queryWrapper.like(ScheduleJob::getJobName, jobName);
            }
        }
        return queryWrapper;
    }


    /**
     * Describe: 新增定时任务
     * Param: ScheduleJob
     * Return: Boolean 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveJob(ScheduleJob record) {
        QuartzService.createJob(scheduler, record);
        int result = scheduleJobMapper.insert(record);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Describe: 修改定时任务
     * Param: ScheduleJob
     * Return: Boolean 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(ScheduleJob record) {
        QuartzService.updateJob(scheduler, record);
        int result = scheduleJobMapper.updateById(record);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Describe: 停止定时任务
     * Param: JobId
     * Return: Boolean 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean pause(String jobId) {
        ScheduleJob scheduleJob = getById(jobId);
        QuartzService.pauseJob(scheduler, Long.parseLong(jobId));
        scheduleJob.setStatus("1");
        int result = scheduleJobMapper.updateById(scheduleJob);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Describe: 恢复定时任务
     * Param: JobId
     * Return: Boolean 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean resume(String jobId) {
        ScheduleJob scheduleJob = getById(jobId);
        QuartzService.resumeJob(scheduler, Long.parseLong(jobId));
        scheduleJob.setStatus("0");
        int result = scheduleJobMapper.updateById(scheduleJob);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Describe: 运行一次定时任务
     * Param: JobId
     * Return: 无返回值
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(String jobId) {
        ScheduleJob scheduleJob = getById(jobId);
        QuartzService.run(scheduler, scheduleJob);
    }

    /**
     * Describe: 删除定时任务
     * Param: JobId
     * Return: Boolean 执行结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String jobId) {
        QuartzService.deleteJob(scheduler, Long.parseLong(jobId));
        int result = scheduleJobMapper.deleteById(jobId);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

}
